import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;


public class MenuBar extends JMenu implements ActionListener
{
	private GrafOrientat graf;
	private JMenuBar meniu;
	private JMenu meniu1,meniu2,meniu3,meniu4;
	private JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9;
	
	MenuBar(GrafOrientat graf)
	{
		this.graf=graf;
		
		meniu = new JMenuBar();
		meniu1= new JMenu("Parcurgeri");			
		meniu2 = new JMenu("Componente");
		meniu3 = new JMenu("Arbore");
		meniu4 = new JMenu("Drum minim");
		
		item1 = new JMenuItem("Parcurgere Generica");
		item2 = new JMenuItem("Parcurgere BF");
		item3 = new JMenuItem("Parcurgere DF");
		item4 = new JMenuItem("Componente conexe");
		item5 = new JMenuItem("Algoritmul Prim");
		item6 = new JMenuItem("Algoritmul Kruskal");
		item7 = new JMenuItem("Algoritmul lui Dijkstra");
		item8 = new JMenuItem("Algoritmul lui Floyd-Warshall");
		item9 = new JMenuItem("Algoritmul lui Bellman-Ford");
		
		item1.addActionListener(this);
		item2.addActionListener(this);
		item3.addActionListener(this);
		item4.addActionListener(this);
		item5.addActionListener(this);
		item6.addActionListener(this);
		item7.addActionListener(this);
		item8.addActionListener(this);
		item9.addActionListener(this);
		
		meniu1.add(item1);
		meniu1.add(item2);
		meniu1.add(item3);
		meniu2.add(item4);
		meniu3.add(item5);
		meniu3.add(item6);
		meniu4.add(item7);
		meniu4.add(item8);
		meniu4.add(item9);
		
		meniu.add(meniu1);
		meniu.add(meniu2);
		meniu.add(meniu3);
		meniu.add(meniu4);
	}
	
	public JMenuBar getMenuBar()
	{
		return meniu;
	}
	

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		JOptionPane j = new JOptionPane();
		if(e.getSource() == item1)
		{
			AlgoritmPTG alg = new AlgoritmPTG();
			String s = alg.getParcurgere(this.graf);
			j.showMessageDialog(item1, s);
		}
		else
			if(e.getSource() == item2)
			{
				AlgoritmPTBF alg = new AlgoritmPTBF();
				String s = alg.getParcurgere(this.graf);
				j.showMessageDialog(item2, s);
			}
			else if(e.getSource() == item3)
			{
				AlgoritmPTDF alg = new AlgoritmPTDF();
				String s = alg.getParcugerePTDF(this.graf);
				j.showMessageDialog(item3, s);
			}
			else
				if(e.getSource() == item4)
				{
					ComponenteConexe comp = new ComponenteConexe(graf);
					String [] c;
					c = comp.getComponenteConexe();
					String co = new String();
					
					co+="Graful are "+c[0]+" componente conexe.";
					for(int i=0;i<Integer.parseInt(c[0]);i++)
						co+="\nComponenta "+String.valueOf(i+1)+": "+c[i+1];
							
					j.showMessageDialog(item4,co);
				}
				else 
					if(e.getSource() == item5)
					{
						AlgoritmPrim alg = new AlgoritmPrim(graf);
						j.showMessageDialog(item5, alg.getArboreMinim());
					}
					else
						if(e.getSource() == item6)
						{
							AlgoritmKruskal alg = new AlgoritmKruskal(graf);
							j.showMessageDialog(item6 , alg.getRezultat());
						}
						else
							if(e.getSource() == item7)
							{
								Dijkstra alg = new Dijkstra(graf);
								String z = j.showInputDialog("Scrie nodul destinatie");
								j.showMessageDialog(item7,alg.getDijkstra(Integer.parseInt(z)));
							}
							else
								if(e.getSource() == item8)
								{
									AlgoritmFloyd_Warshall alg =new AlgoritmFloyd_Warshall(graf);
									int x,y;
									String s;
									s = j.showInputDialog(item8, "Scrieti nodul de start");
									x = Integer.parseInt(s);
									s = j.showInputDialog(item8,"Scrieti nodul destinatie" );
									y = Integer.parseInt(s);
									
									if(x == y)
										j.showMessageDialog(item8, "!!!Nodul destinatie coincide cu nodul de start!!! Scrieti altele.");
									else
									{
										s = alg.getRezultat(x, y);
										j.showMessageDialog(item8, s);
									}
								}
								else
									if(e.getSource() == item9)
									{
										AlgoritmulBellman_Ford alg =new AlgoritmulBellman_Ford(graf);
										String s;
										int x,y;
										s = j.showInputDialog(item9, "Scrieti nodul de start");
										x = Integer.parseInt(s);
										s = j.showInputDialog(item9,"Scrieti nodul destinatie" );
										y = Integer.parseInt(s);
										
										if(x == y)
											j.showMessageDialog(item9, "!!!Nodul destinatie coincide cu nodul de start!!! Scrieti altele.");
										else
										{
											s = alg.getRezultat(x, y);
											j.showMessageDialog(item9, s);
										}
									}
	}
	
}
